---@class UIBattleTopChapterPanel : UIComponentInterface
local UIBattleTopChapterPanel = Class("",UIComponentInterface)
function UIBattleTopChapterPanel:OnCreate(obj)
    self.txtCurLevel = obj:GetChild("CurrentLevel");
    self.txtBeforeLevel = obj:GetChild("BeforeLevel");
    self.txtBeforeBeforeLevel = obj:GetChild("BeforeBeforeLevel");
    self.txtAfterLevel = obj:GetChild("AfterLevel");
    self.txtAfterAfterLevel = obj:GetChild("AfterAfterterLevel");
    self.txtAfter_level = obj:GetChild("After_level")
    self.iconCurLevel = obj:GetChild("CurrentIcon");
    self.iconBeforeLevel = obj:GetChild("BeforeIcon");
    self.iconBeforeBeforeLevel = obj:GetChild("BeforeBeforeIcon")
    self.iconAfterLevel = obj:GetChild("AfterIcon");
    self.iconAfterAfterLevel = obj:GetChild("AfterAfterterIcon");
    self.iconAfter_level = obj:GetChild("After_levelicon")
end
function UIBattleTopChapterPanel:InitMiniMap(obj)
    self.btn_map = obj
    self.treasureController = self.btn_map:GetController("treasure");
    self.btn_map.onClick:Clear()
    self:RegisterOnClick(self.btn_map,self.onClickMap)
end
function UIBattleTopChapterPanel:OnDestroy()
    self:DelReferenceAll()
end
function UIBattleTopChapterPanel:Refresh()
    local mapId = logicMgr.MapManager.getMapId();
    local stageConfig = dataTable.getDataTable("t_stage")
    local current = stageConfig[tostring(mapId)];
    if current.f_NextStage ~= nil and current.f_NextStage > 0 then
        current = stageConfig[tostring(current.f_NextStage)];
        mapId = current.f_StageId;
    end
    if current == nil then return end
    self.txtCurLevel.text = current.f_ChapterId .. "-" .. current.f_StageNum;
    self.iconCurLevel:GetController("iconType").selectedIndex = current.f_type;
    local after = stageConfig[tostring(current.f_NextStage)];
    if after ~= nil then
        self.txtAfterLevel.visible = true;
        self.txtAfterLevel.text = after.f_ChapterId .. "-" .. after.f_StageNum;
        self.iconAfterLevel.visible = true;
        self.iconAfterLevel:GetController("iconType").selectedIndex = after.f_type;
    else
        self.iconAfterLevel.visible = false;
        self.txtAfterLevel.visible = false;
    end
    local afterafter = nil
    if after then
        afterafter = stageConfig[tostring(after.f_NextStage)];
    end
    if afterafter ~= nil then
        self.txtAfterAfterLevel.visible = true;
        self.txtAfterAfterLevel.text = afterafter.f_ChapterId .. "-" .. afterafter.f_StageNum;
        self.iconAfterAfterLevel.visible = true;
        self.iconAfterAfterLevel:GetController("iconType").selectedIndex = afterafter.f_type;
        local after_level = stageConfig[tostring(afterafter.f_NextStage)]
        if after_level ~= nil then
            self.txtAfter_level.text = after_level.f_ChapterId .. "-" .. after_level.f_StageNum;
            self.iconAfter_level.visible = true;
            self.iconAfter_level:GetController("iconType").selectedIndex = after_level.f_type;
        else
            self.iconAfter_level.visible = false;
            self.txtAfter_level.visible = false;
        end
    else
        self.iconAfterAfterLevel.visible = false;
        self.txtAfterAfterLevel.visible = false;
    end
    local before = nil;
    for key, value in pairs(stageConfig) do
        if value.f_NextStage == mapId then
            before = value;
            break ;
        end
    end
    if before ~= nil then
        self.txtBeforeLevel.visible = true;
        if before.f_StageNum == 0 then
            self.txtBeforeLevel.text = "";
        else
            self.txtBeforeLevel.text = before.f_ChapterId .. "-" .. before.f_StageNum;
            --this.iconBeforeLevel.visible = true;
            --this.iconBeforeLevel:GetController("iconType").selectedIndex = before.f_type;
        end
        local beforeBefore = nil;
        for key, value in pairs(stageConfig) do
            if value.f_NextStage == before.f_StageId and value.f_StageId ~= 0 then
                beforeBefore = value;
                break ;
            end
        end
        if beforeBefore ~= nil then
            if beforeBefore.f_ChapterId == 70 and beforeBefore.f_StageNum == 60 then
                self.txtBeforeBeforeLevel.text = "";
            else
                self.txtBeforeBeforeLevel.visible = true;
                self.txtBeforeBeforeLevel.text = beforeBefore.f_ChapterId .. "-" .. beforeBefore.f_StageNum;
                --this.iconBeforeBeforeLevel.visible = true;
                --this.iconBeforeBeforeLevel:GetController("iconType").selectedIndex = beforeBefore.f_type;
            end
        else
            self.txtBeforeBeforeLevel.visible = false;
        end
    else
        self.txtBeforeLevel.visible = false;
        self.txtBeforeBeforeLevel.visible = false;
    end
    self.iconBeforeLevel.visible = false;
    self.iconBeforeBeforeLevel.visible = false;
    --map
    self.treasureController.selectedIndex = 0;
    local taskConfig = logicMgr.TaskManager.getRemarkShowDataByType(EnumConst.AchieveTaskType.Chapter);
    for k, v in pairs(taskConfig) do
        local serverData = logicMgr.TaskManager.getTaskServerData(v.f_TaskId);
        if serverData.status == 1 then
            self.treasureController.selectedIndex = 1;
            break
        end
    end
end

function UIBattleTopChapterPanel:RefreshHard(id)
    local mapId = logicMgr.MapManager.getMapId();
    local stageConfig = dataTable.getDataTable("t_stagehardmode")
    local current = stageConfig[tostring(id)];
    if current.f_NextStage ~= nil and current.f_NextStage > 0 then
        current = stageConfig[tostring(current.f_Id)];
        mapId = current.f_Id;
    end
    if current == nil then return end
    self.txtCurLevel.text = getLanguage(current.f_StageName)
    self.iconCurLevel:GetController("iconType").selectedIndex = 0;
    local after = stageConfig[tostring(current.f_NextStage)];
    if after ~= nil then
        self.txtAfterLevel.visible = true;
        self.txtAfterLevel.text = getLanguage(after.f_StageName)
        self.iconAfterLevel.visible = true;
        self.iconAfterLevel:GetController("iconType").selectedIndex = 0;
    else
        self.iconAfterLevel.visible = false;
        self.txtAfterLevel.visible = false;
    end
    local afterafter = nil
    if after then
        afterafter = stageConfig[tostring(after.f_NextStage)];
    end
    if afterafter ~= nil then
        self.txtAfterAfterLevel.visible = true;
        self.txtAfterAfterLevel.text = getLanguage(afterafter.f_StageName)
        self.iconAfterAfterLevel.visible = true;
        self.iconAfterAfterLevel:GetController("iconType").selectedIndex = 0
        local after_level = stageConfig[tostring(afterafter.f_NextStage)]
        if after_level ~= nil then
            self.txtAfter_level.text = getLanguage(after_level.f_StageName)
            self.iconAfter_level.visible = true;
            self.iconAfter_level:GetController("iconType").selectedIndex = 0;
        else
            self.iconAfter_level.visible = false;
            self.txtAfter_level.visible = false;
        end
    else
        self.iconAfterAfterLevel.visible = false;
        self.txtAfterAfterLevel.visible = false;
    end
    local before = nil;
    for key, value in pairs(stageConfig) do
        if value.f_NextStage == mapId then
            before = value;
            break ;
        end
    end
    if before ~= nil then
        self.txtBeforeLevel.visible = true;
        if before.f_StageNum == 0 then
            self.txtBeforeLevel.text = "";
        else
            self.txtBeforeLevel.text = getLanguage(before.f_StageName)
            --this.iconBeforeLevel.visible = true;
            --this.iconBeforeLevel:GetController("iconType").selectedIndex = before.f_type;
        end
        local beforeBefore = nil;
        for key, value in pairs(stageConfig) do
            if value.f_NextStage == before.f_StageId and value.f_StageId ~= 0 then
                beforeBefore = value;
                break ;
            end
        end
        if beforeBefore ~= nil then
            if beforeBefore.f_ChapterId == 70 and beforeBefore.f_StageNum == 60 then
                self.txtBeforeBeforeLevel.text = "";
            else
                self.txtBeforeBeforeLevel.visible = true;
                self.txtBeforeBeforeLevel.text =  getLanguage(beforeBefore.f_StageName)
                --this.iconBeforeBeforeLevel:GetController("iconType").selectedIndex = beforeBefore.f_type;
            end
        else
            self.txtBeforeBeforeLevel.visible = false;
        end
    else
        self.txtBeforeLevel.visible = false;
        self.txtBeforeBeforeLevel.visible = false;
    end
    self.iconBeforeLevel.visible = false;
    self.iconBeforeBeforeLevel.visible = false;
    --map
    self.treasureController.selectedIndex = 0;
    local taskConfig = logicMgr.TaskManager.getRemarkShowDataByType(EnumConst.AchieveTaskType.Chapter);
    for k, v in pairs(taskConfig) do
        local serverData = logicMgr.TaskManager.getTaskServerData(v.f_TaskId);
        if serverData.status == 1 then
            self.treasureController.selectedIndex = 1;
            break
        end
    end
end

function UIBattleTopChapterPanel:onClickMap()
    if logicMgr.FuncOpenManager.isOpen(Config.FuncOpenType.BigMap, true) then
        showUI(PanelNames.UIBigMap, plunderHandler.have_search);
    end
end
return UIBattleTopChapterPanel